package com.xiaoyu.lanling.feature.visitor.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.base.view.OnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.visitor.model.VisitorItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.UserAvatarDraweeView

class VisitorViewHolder : ViewHolderBase<VisitorItem>() {

    lateinit var avatar: UserAvatarDraweeView
    lateinit var name: TextView
    lateinit var age: TextView
    lateinit var time: TextView
    lateinit var verified: ImageView
    lateinit var vipIcon: SimpleDraweeView

    private val onItemClickListener = object : OnClickDebounceListener() {

        override fun onClickDebounce(v: View) {
            val activity = App.getInstance().topActivity ?: return
            val user = v.getViewTag<User>() ?: return
            Router.instance.gotoUserActivity(activity, user)
        }
    }

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View {
        val convertView = layoutInflater.inflate(R.layout.item_visitor_history, parent, false)
        avatar = convertView.findViewById(R.id.visitor_avatar)
        name = convertView.findViewById(R.id.visitor_name)
        age = convertView.findViewById(R.id.visitor_age)
        time = convertView.findViewById(R.id.visitor_time)
        verified = convertView.findViewById(R.id.visitor_verified)
        vipIcon = convertView.findViewById(R.id.visitor_vip_icon)
        convertView.setOnClickListener(onItemClickListener)
        return convertView
    }

    override fun showData(position: Int, itemData: VisitorItem) {
        ImageViewUtil.loadImage(avatar, itemData.avatarLoadParams)
        ImageViewUtil.loadImage(vipIcon, itemData.vipLoadParam)
        name.text = itemData.user.name
        age.text = itemData.age
        time.text = itemData.time
        name.setTextColor(AppContext.getColor(if (itemData.vipIcon.isNotEmpty()) R.color.vip_name else R.color.colorOnSurface))
        age.setCompoundDrawablesRelativeWithIntrinsicBounds(if (itemData.user.isMale) R.drawable.icon_friend_list_male else R.drawable.icon_friend_list_female, 0, 0, 0)
        age.setBackgroundResource(if (itemData.user.isMale) R.drawable.shape_friend_sex_male else R.drawable.shape_friend_sex_female)
        verified.visibility = if (itemData.verified) View.VISIBLE else View.GONE
        mCurrentView?.setViewTag(itemData.user)
    }
}